'''
Created on Dec 6, 2009

@author: mkiyer
'''
import os
import sys
import logging

from veggie.app.application import CommandLineApplication
from veggie.app.chipseq import sw_root

class SICER(CommandLineApplication):
    '''
    http://home.gwu.edu/~wpeng/Software.htm
    http://groups.google.com/group/sicer-users
    '''
    _executable = os.path.join(sw_root, 'SICER', 'current', 'SICER', 'SICER.sh')

    def get_result_paths(self):
        t_file, c_file, window_size, gap_size, fdr = self.args        
        result_file = '-'.join([t_file.split('.')[0],
                                'W%s' % (window_size),
                                'G%s' % (gap_size),
                                'islands-summary',
                                'FDR%s' % (fdr)])
        return [result_file]

    # output format
    # chrom, start, end, ChIP_island_read_count, CONTROL_island_read_count, p_value, fold_change, FDR_threshold           
    def run(self, t_file, c_file, window_size=200, gap_size=600, fdr="1E-3", options=None, cwd=None):
        if cwd:
            self.cwd = cwd
        if options:
            self.addOptions(options)
        # set environment variables needed by SICER.sh script
        sicer_path = os.path.dirname(self._executable)
        sicer_pythonpath = os.path.join(sicer_path, 'lib')
        self.setenv('SICER', sicer_path)
        self.setenv('PYTHONPATH', ':'.join([sicer_pythonpath, os.environ['PYTHONPATH']]))
        # write command line to file
        logging.debug("Command line: %s" % (self.getCommandLine()))
        self.args = [t_file, c_file, str(window_size), str(gap_size), fdr]
        return self()

